/**
 * 
 */
/**
 * @author shanghaitao
 *
 */
package com.starbaba.system.usergroup.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.starbaba.core.hibernate.DAOFactory;
import com.starbaba.core.hibernate.HibernateDAO;
import com.starbaba.core.service.CommonService;
import com.starbaba.core.service.CommonServiceSupport;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.system.usergroup.model.UsergroupVO;
import com.starbaba.system.usergroup.model.UserGroupModel;
import com.starbaba.system.usergroup.service.UserGroupService;

@Component(value="UserGroupServiceImpl")
public class UserGroupServiceImpl  extends CommonServiceSupport implements UserGroupService {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5108922671733677590L;
	
    @Override
    @SuppressWarnings({ "rawtypes", "unchecked", "unused" })
	public List<UsergroupVO> getDataList(int iDisplayStart, int iDisplayLength,
			UserGroupModel model, PageResponse pageResponse , CommonService server, List groupid, String suittype) {
		StringBuffer sf = new StringBuffer();
		sf.append("from UsergroupVO");
		sf.append(" where 1 = 1 ");
		if(suittype != null){
			if(Integer.valueOf(suittype) == 1){
				if(groupid != null){
					sf.append(" and groupid in('");
					List tempList = new ArrayList(groupid);
					sf.append(StringUtils.join(tempList, "','"));
					tempList.clear();
					sf.append("') ");
				}else{
					sf.append(" and groupid in('-1') ");
				}
			}
		}
		String condition = bulidCondition(model,server);
		sf.append(condition);
		sf.append(" order by id asc");
		String hql = sf.toString();
		String countHql = "select count(*) " + hql;
		HibernateDAO dao = DAOFactory.getInstance().createHibernateDAO(UsergroupVO.class);
		Query query = dao.getSession().createQuery(hql);
		if(iDisplayLength != 0)
		{
			query.setFirstResult(iDisplayStart);
			query.setMaxResults(iDisplayLength);
		}
		//addTimeParm(model,query);
		List<UsergroupVO> list = query.list();
		List<UsergroupVO> returnList = new ArrayList<UsergroupVO>();
		UsergroupVO itemVo;
		SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd");
		Map<Long ,UsergroupVO> map = new HashMap<Long ,UsergroupVO>();
		for(UsergroupVO user :list)
		{
			itemVo = new UsergroupVO();
			itemVo.setGroupid(user.getGroupid());
			itemVo.setComid(user.getComid());
			itemVo.setGroupname(user.getGroupname());
			itemVo.setMemo(user.getMemo());
			itemVo.setCreator(user.getCreator());
			itemVo.setCreatetime(user.getCreatetime());
			itemVo.setLasttime(user.getLasttime());
			itemVo.setStatus(user.getStatus());
			itemVo.setLasteditor(user.getLasteditor());
			returnList.add(itemVo);
		}
		
		Query countQuery = dao.getSession().createQuery(countHql);
		Object obj = countQuery.uniqueResult();
		if(obj instanceof Long)
		{
			int count = ((Long)obj).intValue();
			
			pageResponse.setITotalRecords(count);
			pageResponse.setITotalDisplayRecords(count);
		}
		return returnList;
	}
	
	private String bulidCondition(UserGroupModel model, CommonService server) {
		StringBuffer condition = new StringBuffer();
		
		String groupname = model.getGroupname();
		if (!StringUtils.isEmpty(groupname)) {
			condition.append(" and groupname like '%" + groupname + "%'");
		}
		
		return condition.toString();
	}
}
